/**
 * RPCInterfaceImpl.java
 * 
 * Where all the client-server communication methods are implemented. 
 * This methods implemented here are defined in RPCInterface
 */
package src.ocrs.server;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpSession;

import ocrs.client.RPCInterface;
import registeringSystem.Course;
import registeringSystem.Location;
import registeringSystem.MonthDayYear;
import registeringSystem.OdinsFistSystem;
import registeringSystem.Time;
import registeringSystem.User;
//import ocrs.server.registeringSystem.OdinsFistSystem;
//import ocrs.server.registeringSystem.User;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class RPCInterfaceImpl extends RemoteServiceServlet implements
		RPCInterface {

	private static final long serialVersionUID = 1L;
	private User currentUser;
	private static OdinsFistSystem system;
	HttpSession session; 
	/**
	 * constructor
	 */
	RPCInterfaceImpl(){
		System.out.println("Persisting system...");
		//making session persistent
		//a. get current servlet request
		HttpServletRequest request = this.getThreadLocalRequest();
		//b. get current session (return new session if there isnt one)
		session = request.getSession(true);
		
		//c. get saved system or make new one
		if((system = (OdinsFistSystem)session.getAttribute("system")) != null)
		{
			return;
		}
		else
		{
			session.setAttribute("system", system);
		}
	}
	/**
	 * setSessionUser
	 * set up so same user that logged in is saved
	 */
	private void setSessionUser(){
		//get user saved in session
		currentUser = (User)session.getAttribute("currentUser");
		//if no user saved, create shell user, and notify
		if(currentUser == null)
		{
			currentUser = new User();
			System.err.println("Session User not found. Shell created.");
		}
	}
//	//TODO to retrieve old stuff, (typecast)session.getAttributes("key"); 
//	//TODO to bind objects to a session use setAttributes("")
//
	public boolean createNewCourse(String name, String semester, int year,
			int capacity,int sectionNum, String nameTeach,  
			String daysHeld,Time timeHeld,Location loc,int numWeeks,
			int credits, String excerpt,MonthDayYear start,MonthDayYear end,
			ArrayList<String> preReqs) 
	{
		//take info and create a course
		system.addCourseToSystem(new Course(semester,year,capacity,name,sectionNum,nameTeach,
				loc,daysHeld,timeHeld,numWeeks,credits,excerpt,start,end,preReqs));
		
		
		return false;
	}



	public boolean createNewUser(String userName, String password,
			String email, String address, int permissLevel, String major,
			String minor,boolean isGraduate,String department,String link) {
		
		system.addUserToSystem(new User(userName,address,email,permissLevel,
				password,major,minor,isGraduate,department,link));
		
		return false;
	}
//
	
//	//TODO CANNOT PASS A USER, need to make local user in SYSTEM.checkPassword based on userID passed
	public boolean loginUser(String userID, String password) {
		boolean valid;
		User tempUser;
		//search database to validate user
		tempUser = system.checkThePassword(userID, password);
		
		//if valid, retrieve user from database and save for session, and return true
		if(tempUser!=null){
		//TODO What method do i use to find a user based on Id User?
			return true;
		}
		else{
		//if not valid, return false so UI can reprompt
		return false;
		}
	}
	
	

	
	
	
	
	
	///////////////////TEST METHODS/////////////////////////////
public String helloServer() {
		
		return ("Hullo There!");
	}

	public String saySomething(String message) {
		// TODO Auto-generated method stub
		return " and I say "+ message;
	}

	


}
